package guiClasses;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import objectClasses.Category;
import objectClasses.UserInfo;

public class JudgePanel extends GenericPanel 
{

	ArrayList<Category> categories;
	
	public JudgePanel(final Observer the_o_er) 
	{
		super(the_o_er);
		update(the_o_er);
	}
	
	public void update(final Observer the_o_er)
	{
		String user = GuiMain.getUser().getUserName();
		categories = Category.getCategories();
		ArrayList<Category> cats = new ArrayList<Category>();
		for(int i = 0; i < categories.size(); i++)
		{
			ArrayList<UserInfo> judge_l = categories.get(i).getJudges();
			for(int j = 0; j < judge_l.size(); j++)
			{
				 if(judge_l.get(j).getUserName().equals(user))
				 {
					 cats.add(categories.get(i));
				 }
			}
		}
		JButton[] cat_list = new JButton[cats.size()];
		for(int i = 0; i < cats.size(); i++)
		{
			JButton temp = new JButton();
			temp.setForeground(Color.BLUE);
			final String cat_name = cats.get(i).getName();
			temp.setText(cat_name);
			temp.setBorderPainted(false);
            temp.setOpaque(false);
            temp.setBackground(new Color(139, 101, 219));
            temp.addActionListener(new ActionListener() 
            {
                public void actionPerformed(ActionEvent e) 
                {
                	setChanged();
    				notifyObservers(new CategoryPanel(the_o_er, cat_name).my_panel);
                }
            });
            temp.setCursor(new Cursor(Cursor.HAND_CURSOR));
            cat_list[i] = temp;
		}
		
		JPanel right = new JPanel(new GridBagLayout());
		JLabel cat = new JLabel("You are judging these categories");
		JLabel space = new JLabel(" ");
		Font f = new Font(cat.getFont().getName(),Font.BOLD, 20);  
		cat.setFont(f);
		right.add(cat, GBC.GBCGen(0, 0, 0, 0));
		right.add(space, GBC.GBCGen(0, 0, 0, 1));
		
		JPanel temp = new JPanel(new GridBagLayout());
		for(int i = 0; i < cat_list.length; i++)
		{
			temp.add(cat_list[i], GBC.GBCGen(0, 0, i/6, i % 6));
		}
		JScrollPane scrollPane = new JScrollPane(temp);
		scrollPane.setPreferredSize(new Dimension(400, 190));
		right.add(scrollPane, GBC.GBCGen(0, 0, 0, 2));
		
		my_panel.add(right, GBC.GBCGen(0, 0, 0, 1));
		my_panel.setBorder(new TitledBorder("Event Categories"));
	}

}